package com.atguigu.champter5.Flink05.test;

import com.atguigu.champter5.Flink05.test.bean.UserBehavior;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;

import java.util.HashSet;
import java.util.Set;

public class Flink_UVCount {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env
                .readTextFile("datas/input/UserBehavior.csv")
                .flatMap(new FlatMapFunction<String, UserBehavior>() {
                    @Override
                    public void flatMap(String value, Collector<UserBehavior> out) throws Exception {
                        String[] data = value.split(",");
                        if ("pv".equals(data[3])){
                            out.collect(new UserBehavior(
                                    Long.valueOf(data[0]),
                                    Long.valueOf(data[1]),
                                    Integer.valueOf(data[2]),
                                    String.valueOf(data[3]),
                                    Long.valueOf(data[4])
                            ));
                        }

                    }
                })
                .map(new MapFunction<UserBehavior, Integer>() {
                    Set<Long> userIDs = new HashSet<>();
                    @Override
                    public Integer map(UserBehavior value) throws Exception {
                        userIDs.add(value.getUserId());
                        return userIDs.size();
                    }
                }).print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
